const router = require('express').Router();
const db = require('./../config/dbcofnig')
const upload = require("./../config/uploadconfig")
// 查询医生团队数据
// 团队名称， 负责医生的名称
router.get("/getPageTeam", (req, res) => {
  let { tname, dname } = req.query;


  let sql = "SELECT * FROM `teams` AS t , `teamdoctortable` AS tdt , `doctorstable` AS d  WHERE t.`tid`=tdt.`tid` AND tdt.`did`=d.did";
  let params = [];
  if (!!tname) {
    sql += ` and t.tname = ?`
    params.push(tname)
  }
  if (!!dname) {
    sql += ` and d.dname = ?`
    params.push(dname)
  }

  sql += ";"

  let cb = (err, data) => {
    console.log(sql)
    if (err === null) {
      res.json({ code: 200, msg: '查询成功', data })
    } else {
      res.json({ code: 4001, msg: '查询失败' })
    }
  }

  db.connect(sql, params, cb)
});


// 添加医生团队
router.route("/addDoctorTeam").post(upload.single('tphoto'), (req, res) => {
  // console.log(req.body);
  let { doctorArr, tname, tdes } = req.body;
  let tphoto = req.file?.filename;
  // console.log("doctorArr,tname, tdes,tphoto====+9++++++++=", doctorArr, tname, tdes, tphoto);
  let sql1 = `insert into teams values(?,?,?,?,?);`
  let params1 = [null, tname, tdes, tphoto, '0']
  let cb = (err, data) => {
    console.log(err);
    console.log(data);
    if (err === null) {
      let tid = data.insertId; // 插入的自增长的ID
      res.json({
        code: 200, msg: '添加团队基本信息成功', data: tid
      })

      // 在成功后，继续去添加团队医生信息，对`teamdoctortable`表操作。

    } else {
      res.json({
        code: 200, msg: '添加团队基本信息失败'
      })
    }
  }
  db.connect(sql1, params1, cb)

});

module.exports = router;

